/**
*	插件：ajax使用
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.ajax = {
	post:function(cans,fun){
		cans.type='post';
		cans.url =this.geturls(cans.url, cans.ntoken);
		if(fun)cans.onsuccess=fun;
		this.main(cans,0);
	},
	get:function(cans,fun){
		if(typeof(cans)=='string'){
			var can = {};
			can.url = cans;
			cans 	= can;
		}
		cans.type='get';
		cans.url =this.geturls(cans.url, cans.ntoken);
		if(fun)cans.onsuccess=fun;
		this.main(cans,0);
	},
	main:function(cans,ci){
		var fun = cans.onsuccess,efun = cans.onerror,ztv;
		if(cans.loading)js.show.load(cans.loading);
		if(cans.btnobj){
			ztv = cans.btnobj.innerHTML;
			cans.btnobj.innerHTML = '<i class="rock-loading"></i> '+ztv+'中...';
			cans.btnobj.disabled  = true;
		}
		function eefun(ret){
			if(ret.code==198 && ci<=2){
				setTimeout(function(){js.ajax.main(cans, ci+1)},300);
				return;
			}
			js.show.error(ret.msg);
			if(cans.btnobj){
				cans.btnobj.innerHTML = ztv;
				cans.btnobj.disabled  = false;
			}
			if(ret.code==190){
				js.setoption('token','');
				js.reload();
			}
			if(efun)efun(ret);
		}
		cans.success=function(ret){
			if(ret.success){
				if(cans.loading)js.show.hide();
				if(cans.loadsuccess)js.show.success(cans.loadsuccess);
				if(cans.btnobj){
					cans.btnobj.innerHTML = ztv+'成功';
				}
				if(fun)fun(ret);
			}else{
				eefun(ret);
			}
		}
		cans.error=function(e){
			console.error(e);
			var msg = 'ajaxerror('+e.status+':'+e.statusText+''+e.responseText+')';
			if(e.status==0)msg+='接口地址不能使用';
			eefun({success:false,msg:msg,code:201});
		}
		cans.dataType='json';
		if(cans.cnum){
			var url = cans.url;
			var jg  = this.gjg(url);
			url+=''+jg+'cnum='+cans.cnum+'';
			if(cans.funitid)url+='&funitid='+cans.funitid+'';
			cans.url = url;
		}
		if(cans.urlda){
			var url = cans.url,str='';
			var jg  = this.gjg(url);
			for(var i in cans.urlda)str+='&'+i+'='+cans.urlda[i]+'';
			if(str){
				url+=''+jg+''+str.substr(1)+'';
				cans.url = url;
			}
		}
		if(NOWURL.substr(0,5)=='https' && cans.url.substr(0,5)!='https'){
			cans.error({status:500,statusText:'',responseText:'接口地址必须用https'});
			return;
		}
		$.ajax(cans);
	},
	geturls:function(dz, nto){
		var str = ''+dz+'',jg;
		var url = APIURL+'';
		if(window['APIURLA']){
			var cd = APIURLA.length,xu = parseInt(Math.random()*cd);
			url = APIURLA[xu];
		}
		if(dz.substr(0,4)!='http'){
			if(APITYPE=='php' || url.substr(-3)=='php'){
				jg  = this.gjg(url);
				var arr = dz.split('?');
				str = ''+url+''+jg+'action='+arr[0]+'';
				if(arr[1])str+='&'+arr[1]+'';
			}else{
				str = ''+url+str+'';
			}
		}
		jg  = this.gjg(str);
		if(!nto)str+=''+jg+'token='+token+'';
		jg  = this.gjg(str);
		str+=''+jg+'cfrom='+CFROM+'';
		return str;
	},
	gjg:function(vst){
		var jg  = (vst.indexOf('?')==-1) ? '?' : '&';
		return jg;
	},
	geturl:function(dz,cans){
		var str = this.geturls(dz, false);
		var jg  = this.gjg(str);
		if(cans){
			var st1 = '';
			for(var i in cans)st1+='&'+i+'='+cans[i]+'';
			if(st1)str+=''+jg+''+st1.substr(1)+'';
		}
		return str;
	}
}